//
//  OrderViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "OrderViewController.h"

#import "JianLiViewController.h"
#import "XDJCViewController.h"
#import "GXViewController.h"
#import "ADDSPViewController.h"
#import "SuGuoViewController.h"
#import "KaiSuoViewController.h"
#import "JiaZhenViewController.h"
#import "KLDPViewController.h"
#import "KuaiDiViewController.h"
#import "UIColor+Extension.h"
#import "ShangHuViewController.h"
@interface OrderViewController ()<BMKLocationServiceDelegate>
{
    NSArray *arr1;
    NSArray *arr2;
    BMKLocationService *location;
    CGFloat lat;
    CGFloat lng;
    NSMutableArray *array;
}
@end

@implementation OrderViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    arr1 = @[@"工人-拷贝",@"干洗",@"快递",@"家政员",@"11321",@"锁打开_块",@"果蔬-",@"远程教学",@"简历操作"];
    arr2 = @[@"苦力",@"干洗",@"快递",@"家政",@"",@"开锁",@"蔬果",@"信贷教程",@"简历查询"];
    array = [NSMutableArray array];
    [self loadNav];
    [self loadMap];
    [self loadBanner];
    
    //[self loadUI];
    
}



- (void)loadMap{
    location = [[BMKLocationService alloc] init];
    [location startUserLocationService];
    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    location.delegate = self;
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    location.delegate = nil;
}

#pragma -mark BMKLocationServiceDelegate
-(void)didUpdateBMKUserLocation:(BMKUserLocation *)userLocation{
    NSLog(@"didUpdateUserLocation lat %f,long %f",userLocation.location.coordinate.latitude,userLocation.location.coordinate.longitude);
    lat = userLocation.location.coordinate.latitude;
    lng = userLocation.location.coordinate.longitude;
    [location stopUserLocationService];
    
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"叫单";
    lab.textColor = [UIColor blackColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    UILabel *line = [[UILabel alloc] init];
    line.backgroundColor = [UIColor lightGrayColor];
    [view addSubview:line];
    [line makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.bottom.equalTo(view.bottom);
        make.height.equalTo(0.5);
    }];
}

- (void)loadUI{
    UIView *imageView = [WTImageScroll ShowNetWorkImageScrollWithFream:CGRectMake(0, 64, ScreenWidth, 170) andImageArray:array andBtnClick:^(NSInteger tagValue) {
        
    }];
    [self.view addSubview:imageView];
    
    CGFloat width = ScreenWidth/3;
    CGFloat height = (ScreenHeight-64-170-49)/3;
    //for循环创建9个按钮
    for (NSInteger i = 0; i<9; i++) {
        UIView *view = [[UIView alloc] initWithFrame:CGRectMake(width*i, imageView.frame.origin.y+imageView.frame.size.height, width, height)];
        view.userInteractionEnabled = YES;
        [self.view addSubview:view];
        //添加手势
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
        [view addGestureRecognizer:tap];
        
        UIView *v = [tap view];
        v.tag = i;
        
        //图标
        UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arr1[i]]];
        [view addSubview:image1];
        [image1 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view.centerX);
            make.centerY.equalTo(view.centerY).offset(-10);
            make.width.height.mas_equalTo(55);
        }];
        //标题
        UILabel *lab1 = [[UILabel alloc] init];
        lab1.text = arr2[i];
        lab1.font = [UIFont systemFontOfSize:14];
        lab1.textColor = [UIColor hexColor:@"262626"];
        lab1.textColor = COLOR_RGB(38, 38, 38, 1);
        [view addSubview:lab1];
        [lab1 makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(view.centerX);
            make.top.equalTo(image1.bottom).offset(10);
        }];
        
        if (i<6) {//3,4,5
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(width*(i-3), height+imageView.frame.origin.y+imageView.frame.size.height, width, height)];
            view.tag = i;
            view.userInteractionEnabled = YES;
            [self.view addSubview:view];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
            [view addGestureRecognizer:tap];
            
            UIView *v = [tap view];
            v.tag = i;
            
            if (i == 4) {
                UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arr1[i]]];
                [view addSubview:image1];
                [image1 makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(view.centerX);
                    make.centerY.equalTo(view.centerY).offset(10);
                }];
                
            }else{
                UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arr1[i]]];
                [view addSubview:image1];
                [image1 makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(view.centerX);
                    make.centerY.equalTo(view.centerY).offset(-10);
                }];
                
                UILabel *lab1 = [[UILabel alloc] init];
                lab1.text = arr2[i];
                lab1.font = [UIFont systemFontOfSize:14.0];
                lab1.textColor = [UIColor hexColor:@"262626"];
                [view addSubview:lab1];
                [lab1 makeConstraints:^(MASConstraintMaker *make) {
                    make.centerX.equalTo(view.centerX);
                    make.top.equalTo(image1.bottom).offset(8);
                }];
            }
            
        }else if (i<9){
            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(width*(i-6), height*2+imageView.frame.origin.y+imageView.frame.size.height, width, height)];
            view.tag = i;
            view.userInteractionEnabled = YES;
            [self.view addSubview:view];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(click:)];
            [view addGestureRecognizer:tap];
            
            UIView *v = [tap view];
            v.tag = i;
            
            UIImageView *image1 = [[UIImageView alloc] initWithImage:[UIImage imageNamed:arr1[i]]];
            [view addSubview:image1];
            [image1 makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(view.centerX);
                make.centerY.equalTo(view.centerY).offset(-10);
            }];
            
            UILabel *lab1 = [[UILabel alloc] init];
            lab1.text = arr2[i];
            lab1.font = [UIFont systemFontOfSize:14.0];
            lab1.textColor = [UIColor hexColor:@"262626"];
            [view addSubview:lab1];
            [lab1 makeConstraints:^(MASConstraintMaker *make) {
                make.centerX.equalTo(view.centerX);
                make.top.equalTo(image1.bottom).offset(8);
            }];
            
//            if (i == 7) {
//                image1.hidden = YES;
//                lab1.hidden = YES;
//                view.userInteractionEnabled = NO;
//            }
            
        }
        
    }
    
}

- (void)click:(id)sender{
    UITapGestureRecognizer *tap = (UITapGestureRecognizer *)sender;
    
    if ([tap view].tag == 0) {
        KLDPViewController *kuli = [[KLDPViewController alloc] init];
        kuli.lat = [NSString stringWithFormat:@"%f",lat];
        kuli.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:kuli animated:YES];
    }else if ([tap view].tag == 1){
        GXViewController *gx = [[GXViewController alloc] init];
        gx.lat = [NSString stringWithFormat:@"%f",lat];
        gx.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:gx animated:YES];
    }else if ([tap view].tag == 2){
        KuaiDiViewController *kd = [[KuaiDiViewController alloc] init];
        kd.lat = [NSString stringWithFormat:@"%f",lat];
        kd.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:kd animated:YES];
    }else if ([tap view].tag == 3){
        JiaZhenViewController *jiazhen = [[JiaZhenViewController alloc] init];
        jiazhen.lat = [NSString stringWithFormat:@"%f",lat];
        jiazhen.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:jiazhen animated:YES];
    }else if ([tap view].tag == 4){
        
    }else if ([tap view].tag == 5){
        KaiSuoViewController *kaisuo = [[KaiSuoViewController alloc] init];
        kaisuo.lat = [NSString stringWithFormat:@"%f",lat];
        kaisuo.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:kaisuo animated:YES];
    }else if ([tap view].tag == 6){
        SuGuoViewController *sg = [[SuGuoViewController alloc] init];
        sg.lat = [NSString stringWithFormat:@"%f",lat];
        sg.lng = [NSString stringWithFormat:@"%f",lng];
        [self.navigationController pushViewController:sg animated:YES];
    }else if ([tap view].tag == 7){
        XDJCViewController *xdjc = [[XDJCViewController alloc] init];
        [self.navigationController pushViewController:xdjc animated:YES];
    }else{
        JianLiViewController *jianli = [[JianLiViewController alloc] init];
        [self.navigationController pushViewController:jianli animated:YES];
    }
    
}
//轮播图
- (void)loadBanner{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *url = [NSString stringWithFormat:@"%@/App/Banner/calledsingle",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        NSString *error = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"error"] integerValue]];
        if ([error integerValue] == 200) {
            NSArray *arr = [responseObject objectForKey:@"result"];
            for (NSDictionary *dic in arr) {
                NSString *imageUrl = [NSString stringWithFormat:@"%@%@",[user objectForKey:@"ImageUrl"],[dic objectForKey:@"banner"]];
                [array addObject:imageUrl];
                [self loadUI];
            }
            NSLog(@"array:%@",array);
        }
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}
/*
 1 购车-工具栏 2 购房-工具栏 3 叫单-工具栏 4 购车- 5 洗车 6 上户 7 车险 8 买房 9 租房 10 苦力 11 干洗 12 快递 13 家政 14 开锁 15 蔬果 16 信贷教程 17 简历查询       目录类型
 */
//丫丢丢-系统配置
- (void)loadData{
    NSString *url = [NSString stringWithFormat:@"%@/App/AppConfig/config",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        //is_show 2，显示 3，隐藏
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
